package com.huatai.common.controller;

import com.huatai.common.entity.UserInfo;
import com.huatai.common.util.Func;
import com.huatai.common.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @program: ht-system
 * @description: 基础控制器
 * @author: songxiude
 * @create: 2023-02-08 14:24
 **/
public class BaseController {
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private JwtUtils jwtUtils;

	public BaseController() {
	}

	public HttpServletRequest getRequest() {
		return this.request;
	}

	/**
	 * 获取登录账号信息
	 * @return
	 */
	public UserInfo getUser(){
		UserInfo userInfo=new UserInfo();
		String token = request.getHeader("Ht-auth");
		Claims claims = jwtUtils.parseJwt(token);
		if(Objects.isNull(claims)){
			return null;
		}

		userInfo.setUserId(Func.toLong(claims.get("user_id")));
		userInfo.setName(Func.toStr(claims.get("name")));
		userInfo.setCode(Func.toStr(claims.get("user_code")));
		userInfo.setAccount(Func.toStr(claims.get("username")));
		userInfo.setDeptId(Func.toLong(claims.get("dept_id")));
		userInfo.setPostId(Func.toLong(claims.get("post_id")));
		userInfo.setRealName(Func.toStr(claims.get("real_name")));
		return userInfo;
	}
}
